/*
 * @lc app=leetcode.cn id=641 lang=javascript
 *
 * [641] 设计循环双端队列
 */

// @lc code=start
/**
 * @param {number} k
 */
/**
 * @param {number} k
 */
 var MyCircularDeque = function(k) {
    this.max = k + 1;
    this.list = new Array(this.max);
    this.front = 0;
    this.rear = 0;
 };
 
 /** 
  * @param {number} value
  * @return {boolean}
  */
 MyCircularDeque.prototype.insertFront = function(value) {
    if (this.isFull()) return false;
    this.front = (this.front - 1 + this.max) % this.max
    this.list[this.front] = value;
    return true; 
 };
 
 /** 
  * @param {number} value
  * @return {boolean}
  */
 MyCircularDeque.prototype.insertLast = function(value) {
    if (this.isFull()) return false;
    this.list[this.rear] = value;
    this.rear = (this.rear + 1) % this.max;
    return true;
 };
 
 /**
  * @return {boolean}
  */
 MyCircularDeque.prototype.deleteFront = function() {
    if (this.isEmpty()) return false;
    this.front = (this.front + 1) % this.max;
    return true;
 };
 
 /**
  * @return {boolean}
  */
 MyCircularDeque.prototype.deleteLast = function() {
    if (this.isEmpty()) return false;
    this.rear = (this.rear - 1 + this.max) % this.max;
    return true;
 };
 
 /**
  * @return {number}
  */
 MyCircularDeque.prototype.getFront = function() {
    if (this.isEmpty()) return -1;
    return this.list[this.front];
 };
 
 /**
  * @return {number}
  */
 MyCircularDeque.prototype.getRear = function() {
   if (this.isEmpty()) return -1;
   return this.list[(this.rear - 1 + this.max) % this.max];
 };
 
 /**
  * @return {boolean}
  */
 MyCircularDeque.prototype.isEmpty = function() {
    return this.front === this.rear;
 };
 
 /**
  * @return {boolean}
  */
 MyCircularDeque.prototype.isFull = function() {
    return (this.rear + 1) % this.max === this.front;
 };
 

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * var obj = new MyCircularDeque(k)
 * var param_1 = obj.insertFront(value)
 * var param_2 = obj.insertLast(value)
 * var param_3 = obj.deleteFront()
 * var param_4 = obj.deleteLast()
 * var param_5 = obj.getFront()
 * var param_6 = obj.getRear()
 * var param_7 = obj.isEmpty()
 * var param_8 = obj.isFull()
 */
// @lc code=end
